//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Copyright 2012 Microsoft Corporation. All Rights Reserved
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.   
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

namespace Preflight.Verifications
{
    using System;
    using System.ComponentModel.Composition;
    using Model;

    /// <summary>
    /// VerificationBase provides an exportable type so that reusable verifications can be instantiated by 
    /// VerificationBuilder
    /// </summary>
    /// <typeparam name="TDescription">Verification Description</typeparam>
    [InheritedExport]
    public abstract class VerificationBase<TDescription> : IVerification where TDescription : VerificationDescription
    {
        public string Name
        {
            get
            {
                return this.Description.VerificationName;
            }
        }

        public string Category
        {
            get
            {
                return this.Description.Category;
            }
        }

        protected TDescription Description
        {
            get;
            private set;
        }

        public VerificationResult Run()
        {
            var result = new VerificationResult(this);

            if (this.TryOnBeforeVerification(result) == false)
            {
                result.AddMessage(MessageType.Error, "Verification Pre-Check did not pass");
                return result;
            }

            this.AddVerificationDetail(result);
            this.TryExecuteVerification(result);
            return result;
        }

        internal void Initialize(TDescription description)
        {
            this.Description = description;
        }

        /// <summary>
        /// Invoked prior to verification execution. This is suitable to validate that the configuration
        /// </summary>
        /// <param name="result">Verification result</param>
        /// <returns>True to proceed. False will prevent verification from executing and mark it as failed</returns>
        protected abstract bool OnBeforeVerification(VerificationResult result);

        /// <summary>
        /// Adds details about what is being verified
        /// </summary>
        /// <param name="result">Verification result</param>
        protected abstract void AddVerificationDetail(VerificationResult result);

        /// <summary>
        /// Executes the verification
        /// </summary>
        /// <param name="result">verification result</param>
        protected abstract void ExecuteVerification(VerificationResult result);

        private void TryExecuteVerification(VerificationResult result)
        {
            try
            {
                this.ExecuteVerification(result);
            }
            catch (Exception exception)
            {
                result.AddException(exception);
            }
        }

        private bool TryOnBeforeVerification(VerificationResult result)
        {
            try
            {
                return this.OnBeforeVerification(result);
            }
            catch (Exception exception)
            {
                result.AddException(exception);
                return false;
            }
        }
    }
}
